<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Organization;
use App\Models\User;

class OrganizationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 创建总公司
        $headquarters = Organization::create([
            'name' => '总公司',
            'code' => 'HQ',
            'description' => '公司总部，负责全局战略规划和管理',
            'address' => '北京市朝阳区CBD商务中心A座',
            'phone' => '010-12345678',
            'email' => 'contact@headquarters.com',
            'website' => 'https://www.headquarters.com',
            'is_active' => true,
        ]);
        
        // 创建研发部门
        $rd = Organization::create([
            'name' => '研发部',
            'code' => 'RD',
            'description' => '负责产品研发和技术创新',
            'address' => '北京市海淀区中关村科技园',
            'phone' => '010-87654321',
            'email' => 'rd@headquarters.com',
            'website' => 'https://rd.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建市场部门
        $marketing = Organization::create([
            'name' => '市场部',
            'code' => 'MKT',
            'description' => '负责市场推广和品牌建设',
            'address' => '北京市朝阳区CBD商务中心A座',
            'phone' => '010-12345679',
            'email' => 'marketing@headquarters.com',
            'website' => 'https://marketing.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建财务部门
        $finance = Organization::create([
            'name' => '财务部',
            'code' => 'FIN',
            'description' => '负责财务管理和资金规划',
            'address' => '北京市朝阳区CBD商务中心A座',
            'phone' => '010-12345680',
            'email' => 'finance@headquarters.com',
            'website' => 'https://finance.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建人力资源部门
        $hr = Organization::create([
            'name' => '人力资源部',
            'code' => 'HR',
            'description' => '负责人才招聘和员工发展',
            'address' => '北京市朝阳区CBD商务中心A座',
            'phone' => '010-12345681',
            'email' => 'hr@headquarters.com',
            'website' => 'https://hr.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建上海分公司
        $shanghai = Organization::create([
            'name' => '上海分公司',
            'code' => 'SH',
            'description' => '负责华东地区业务',
            'address' => '上海市浦东新区陆家嘴金融中心',
            'phone' => '021-12345678',
            'email' => 'shanghai@headquarters.com',
            'website' => 'https://shanghai.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建广州分公司
        $guangzhou = Organization::create([
            'name' => '广州分公司',
            'code' => 'GZ',
            'description' => '负责华南地区业务',
            'address' => '广州市天河区珠江新城',
            'phone' => '020-12345678',
            'email' => 'guangzhou@headquarters.com',
            'website' => 'https://guangzhou.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 创建成都分公司
        $chengdu = Organization::create([
            'name' => '成都分公司',
            'code' => 'CD',
            'description' => '负责西南地区业务',
            'address' => '成都市高新区天府大道',
            'phone' => '028-12345678',
            'email' => 'chengdu@headquarters.com',
            'website' => 'https://chengdu.headquarters.com',
            'is_active' => true,
            'parent_id' => $headquarters->id,
        ]);
        
        // 获取用户并分配到组织
        $admin = User::where('email', 'admin@example.com')->first();
        $editor = User::where('email', 'editor@example.com')->first();
        $user = User::where('email', 'user@example.com')->first();
        
        if ($admin) {
            // 管理员是总公司和所有部门的管理员
            $headquarters->users()->attach($admin->id, ['is_admin' => true]);
            $rd->users()->attach($admin->id, ['is_admin' => true]);
            $marketing->users()->attach($admin->id, ['is_admin' => true]);
            $finance->users()->attach($admin->id, ['is_admin' => true]);
            $hr->users()->attach($admin->id, ['is_admin' => true]);
            $shanghai->users()->attach($admin->id, ['is_admin' => true]);
            $guangzhou->users()->attach($admin->id, ['is_admin' => true]);
            $chengdu->users()->attach($admin->id, ['is_admin' => true]);
        }
        
        if ($editor) {
            // 编辑是研发部和市场部的管理员，其他部门的普通成员
            $headquarters->users()->attach($editor->id, ['is_admin' => false]);
            $rd->users()->attach($editor->id, ['is_admin' => true]);
            $marketing->users()->attach($editor->id, ['is_admin' => true]);
            $finance->users()->attach($editor->id, ['is_admin' => false]);
            $hr->users()->attach($editor->id, ['is_admin' => false]);
        }
        
        if ($user) {
            // 普通用户是研发部的普通成员
            $rd->users()->attach($user->id, ['is_admin' => false]);
        }
        
        $this->command->info('示例组织数据已创建！');
    }
}